nameSpace("commodity");

function Trim(s){
	return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
}

/**
 * 条件
 * 
 * @type
 */
commodity.condition = {
	order : 0,
	lableName :'',
	pageSize : 60,
	pageNo : 1
	
};


/**
 * 排序
 * 
 * @param {}
 *            orderSortId
 */
commodity.order = function(orderSortId) {
	if (orderSortId == 'hot_sort_id') { // 贴条
		$('#time_sort_id').removeClass('sortselected');
		$('#time_sort_id').removeClass('ascending');
		if ($('#' + orderSortId).hasClass('sortselected')) {
			if ($('#' + orderSortId).hasClass('ascending')) {
				commodity.condition.order = 3;
				$('#' + orderSortId).removeClass('ascending');
			} else {
				commodity.condition.order = 2;
				$('#' + orderSortId).addClass('ascending');
			}
		} else {
			$('#' + orderSortId).addClass('sortselected');
			if ($('#' + orderSortId).hasClass('ascending')) {
				commodity.condition.order = 2;
			} else {
				commodity.condition.order = 3;
			}
		}
	} else { // 时间
		$('#hot_sort_id').removeClass('sortselected');
		$('#hot_sort_id').removeClass('ascending');
		if ($('#' + orderSortId).hasClass('sortselected')) {
			if ($('#' + orderSortId).hasClass('ascending')) {
				commodity.condition.order = 0;
				$('#' + orderSortId).removeClass('ascending');
			} else {
				commodity.condition.order = 1;
				$('#' + orderSortId).addClass('ascending');
			}
		} else {
			$('#' + orderSortId).addClass('sortselected');
			if ($('#' + orderSortId).hasClass('ascending')) {
				commodity.condition.order = 1;
			} else {
				commodity.condition.order = 0;
			}
		}
	}
	commodity.loadCommodityInfoByUserId();
};


commodity.loadCommodityInfoByUserId = function(){
	var url = "index_commodity_loadCommodityInfo.htm";
	commodity.loadCommodityInfo(url);
}

/**
 * 加载列表数据
 */
commodity.loadCommodityInfo = function(url) {
	$("#commodity_info_id").html("正在载入数据....");
	$.ajax({
		   type: "POST",
		   dataType:'html',
		   data:commodity.condition,
		   cache:false,
		   url: url,
		   success:function(data){
		   		$("#commodity_info_id").html(data);
		   		$("#commodity_info_id img").lazyload({effect:"fadeIn"} );
		   }
	});
};

commodity.lableOnclick = function(lableName){
	commodity.condition.lableName=lableName;
	var url = "index_commodity_loadCommodityInfo.htm?lableName="+encodeURI(encodeURI(lableName));
	commodity.loadCommodityInfo(url);
}

/**
 * 加载标签数据
 */
commodity.loadLableInfo = function(commodityId,commodityTitle,imgsrc) {
	$.ajax({
		type: "POST",
		dataType:'html',
		data:{commodityId:commodityId,
			  commodityTitle:commodityTitle,
			  imgsrc:imgsrc} ,
		cache:false,
		url: "index_commodity_loadLableInfo.htm",
		success:function(data){
			$("#commodity_lable_id").html(data);	
		}
	});
};

commodity.contentOnfocus = function(){
	$("#comment_id").hide();
};

commodity.contentOnBlur = function(value){
	if(Trim(value)==""){
		$("#comment_id").show();
	}
};

commodity.loadLableInfoList = function() {
	$("#lable_info").html("正在载入数据....");
	$.ajax({
		type: "POST",
		dataType:'html',
		cache:false,
		url: "index_commodity_lableInfo.htm",
		success:function(data){
			$("#lable_info").html(data);	
		}
	});
};

commodity.recommendOnclick = function(){
	var commodityId = $("#commodityId").val();
	$.post('index_commodity_commend.htm',{commodityId:commodityId},function(data){
		$.dialog({
		    padding: 0,
		    id:"commodity_Id_"+commodityId,
		    width:600,
		    title: '贴条',
		    content: data,
		    lock: true
		});
	});
};

commodity.selectArr= function(){
	$("input[name='userCheckbox']").attr("checked",true);
};

commodity.commendUser=function(){
	var commodityId = $("#commodityId").val();
	var checkArr = $("input[name='userCheckbox']");
	var flag = false;
	for(var i=0;i<checkArr.length;i++){
		if(checkArr[i].checked ==true){
			flag=true;
		}
	}
	if(flag){
		$.ajax({
			type: "POST",
			data:{'commodityId':commodityId},
			cache:false,
			url: "index_commodity_commendUser.htm?"+$("#commodityUserForm").serialize(),
			success:function(){
				alert("提交成功！");
				$.dialog.list['commodity_Id_'+commodityId].close();	
			}
		});
	}else{
		alert("请选择要推荐的人！");
		return false;
	}
};

commodity.commentOnclick=function(){
	var commodityId = $("#commodityId").val();
	var commentInfo = $("#commentInfo").val();
	if(Trim(commentInfo)==""){
		alert("请输入评论内容！");
		return false;
	}
	$.ajax({
		type: "POST",
		data:{'commodityId':commodityId,'commentInfo':commentInfo},
		cache:false,
		url: "index_commodity_submitComment.htm",
		success:function(){
			commodity.loadLableInfo(commodityId)
		}
	});
}

commodity.replyOnclick = function(commentId){
	$("#reply_"+commentId).addClass("win-rreply");
};

commodity.submitReplyInfo = function(commentId, parentCommentId){
	var commodityId = $("#commodityId").val();
	var contentInfo = $("#content_"+commentId).val();
	if(Trim(contentInfo)==""){
		alert("请输入回复内容！");
		return false;
	}
	$.ajax({
		type: "POST",
		data:{'commodityId':commodityId,'commentId':parentCommentId,'contentInfo':contentInfo},
		cache:false,
		url: "index_commodity_submitReplyInfo.htm",
		success:function(){
			commodity.loadLableInfo(commodityId)
		}
	});
};

commodity.loadAllLable = function(){
	$("#lable_info1").attr("style","display:none;");
	$("#lable_info2").attr("style","display:'';");
}

commodity.search = function(){
	var petName = $("#search_petName").val();
	commodity.loadFriendList(petName);
}

commodity.loadFriendList = function(petName){
	$.ajax({
		type: "POST",
		dataType:'html',
		data:{'petName':petName},
		cache:false,
		url: "index_commodity_friendList.htm",
		success:function(data){
			$("#user_dl").html(data);	
		}
	});
}
